<?php

namespace App\Http\Controllers\Frontend\V10\Setting;

use App\Constants\RedisKey;
use App\Http\Controllers\Controller;
use App\Models\SettingBannerImage;
use App\Models\SettingConfig;
use App\Models\SettingDocument;
use App\Models\SettingHint;
use App\Models\SettingHomeImage;
use App\Models\SettingHomeInsurance;
use App\Models\SettingLoan;
use App\Models\SettingPopupSwitch;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class SettingInfoController extends Controller
{
    /**
     * Description:获取首页的轮播图图片地址
     * Author: ljf
     * DateTime: 2018/12/27 4:05
     * @return \Illuminate\Http\JsonResponse
     */
    public function getHomeImageInfo()
    {

        $imageInfo = \Cache::remember(RedisKey::SETTING_HOME_IMAGE, 60 * 24 * 30, function () {
            return SettingHomeImage::select('img_url')
                ->where('is_show', 1)
                ->get();
        });
        //添加oss域名
        foreach ($imageInfo as $k => &$v) {
            if (!empty($v['img_url'])) {
                $v['img_url'] = getOssUrl($v['img_url']);
            }
        }
        $homeImageTime = \Cache::remember(RedisKey::SETTING_CONFIG, 60 * 24 * 30, function () {
            return SettingConfig::select('home_image_time')->first();
        });
        $returnData['image_info'] = $imageInfo;
        $returnData['home_image_time'] = $homeImageTime->home_image_time;
        return success($returnData);
    }

    /**
     * Description:获取首页扒窗图片地址
     * Author: wjl
     * DateTime: 2019-04-10 17:01
     * @return mixed
     */
    public function getBannerImageInfo()
    {
        $obj = SettingBannerImage::select('img_url', 'title', 'url')->where('is_show', 1)
            ->first();

        if (is_null($obj)) {
            Log::debug("获取扒窗失败");
            return success(['is_show'=>2]);;
        }

        //添加oss域名
        $obj->img_url = getOssUrl($obj->value('img_url'));
        $obj->url = is_null($obj->url) ? '' : $obj->url;
        $obj->is_show = 1;
        return success($obj);
    }

    /**
     * Description: 弹窗开关获取
     * Author: wjl
     * DateTime: 2019-04-11 20:44
     * @return mixed
     */
    public function getPopupSwitchInfo(Request $request)
    {
        $channel = intval($request->get('channel'));
        if (!in_array($channel, array_keys(SettingPopupSwitch::switchType()))) {
            Log::debug("弹窗开关获取失败--参数错误");
            return failed('参数错误');
        }

        $obj = SettingPopupSwitch::select('id', 'title', 'status', 'url', 'desc', 'channel')->where('channel', $channel)
            ->first();

        if (is_null($obj)) {
            Log::debug("弹窗开关获取失败");
            return success(['status'=>'off','title'=>'','url'=>'','desc'=>'']);
        }

        $obj->status = $obj->status == 1 ? 'on' : 'off';
        $obj->url = is_null($obj->url) ? '' : $obj->url;
        $child = SettingPopupSwitch::select('id',  'title', 'status', 'url', 'desc')->where('parent_id', $obj->channel)->first();

        if ($child) {
            $obj->has_child = 'yes';
            $child->url = is_null($child->url) ? '' : $child->url;
            $child->status = $child->status == 1 ? 'on' : 'off';
            $obj->child = $child;
        } else {
            $obj->has_child = 'no';
        }
        return success($obj);
    }

    /**
     * Description:获取首页的下面的H5页面的列表信息
     * Author:lijiafei
     * @return mixed
     */
    public function getHomeInsuranceInfo()
    {
        $insuranceInfo = \Cache::remember(RedisKey::SETTING_HOME_INSURANCE, 60 * 24 * 30, function () {
            return $insuranceInfo = SettingHomeInsurance::select('img_url', 'title', 'subtitle', 'button_name', 'url_set')->orderBy('id', 'desc')->get();
        });
        //添加oss域名
        foreach ($insuranceInfo as $k => &$v) {
            $v['img_url'] = getOssUrl($v['img_url']);
        }
        return success($insuranceInfo);
    }

    /**
     * Description: 获取借款等的系统参数
     * Author:lijiafei
     * DataTime:2018/12/27 5:16
     */
    public function getLoanSetting()
    {
        $loanSetting = \Cache::remember(RedisKey::SETTING_LOAN, 60 * 24 * 30, function () {
            return $loanSetting = SettingLoan::select('loan_range', 'period_range')->first();
        });
        $loan_range = json_decode($loanSetting->loan_range, true);
        $setting_hint = SettingHint::where('type', 21)->value('status');
        $prompt_box = SettingHint::where('type', 22)->value('status');
        $data = [
            'max' => $loan_range[1],
            'min' => $loan_range[0],
            'period_range' => json_decode($loanSetting->period_range, true),
            'rate' => config('common.default_rate'),
            'status' => $setting_hint,
            'prompt_box' => $prompt_box,
        ];
        return success($data);
    }

    /**
     * 获取app的提示语
     */
    public function getHint(Request $request, $type = '')
    {
        $channel_code = '';
        if (!empty($request->user())) {
            $channel_code = $request->user()->request_channel;
        }
        $hintInfo = SettingHint::getHint($type, $channel_code);
        return success($hintInfo);

    }

    /**
     * 获取文档
     */
    public function getDocument(Request $request, $type = '')
    {

//        RepaySummary::getRepayPlan(1);
        $channel_code = '';
        if (!empty($request->user())) {
            $channel_code = $request->user()->request_channel;
        }
        $list = SettingDocument::getDocumentByType($type, $channel_code);
        return success($list);
    }

}
